<?php
require_once "managerSession.php";
$Item="会员管理";
if(isset($_REQUEST["page"])){
    $page=$_REQUEST["page"];
}else{
    $page=1;
}
if(isset($_REQUEST["close"])){
    $core->CloseUser($_REQUEST["close"]);
}

$search="";
$pageCount=0;
$arr=[];
if (isset($_REQUEST["search"])&&!empty($search=trim($_REQUEST["search"]))){
    $pageCount=ceil($core->GetUserBySearchCount($search)/20);
    $arr=$core->GetUserBySearch($search,$page);
}else{
    $pageCount=ceil($core->GetNormalUsersCount()/20);
    $arr=$core->GetNormalUsers($page);
}

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>娱乐会所吧台管理系统</title>
    <?php include_once "link.php"; ?>
</head>
<body class="container">
<?php require_once "Nav.php"; ?>
<div class="main">
    <?php include_once "menu.php"; ?>
    <div class="pr inlb bd col-xs-10">
        <table class="table table-striped">
            <caption>
                会员管理
                <form id="search" class="pull-right" action="">
                    <input type="text" name="search" value="<?php echo $search; ?>">
                    <button>搜索</button>
                </form>
            </caption>
            <tr>
                <th>姓名</th>
                <th>微信</th>
                <th>手机</th>
                <th>总消费</th>
                <th>总佣金</th>
                <th>操作</th>
            </tr>
            <?php

            for($i=0;$i<count($arr);++$i){
            ?>
            <tr>
                <td><?php echo $arr[$i]["name"]; ?></td>
                <td><?php echo $arr[$i]["weixin_Id"]; ?></td>
                <td><?php echo $arr[$i]["phone"]; ?></td>
                <td><button class="btn-link" onclick="expend(<?php echo $arr[$i]["id"]; ?>)" data-toggle="modal" data-target="#show"><?php echo $arr[$i]["expend"]; ?></button></td>
                <td><button class="btn-link" onclick="bonus(<?php echo $arr[$i]["id"]; ?>)" data-toggle="modal" data-target="#show"><?php echo $arr[$i]["bonus"]; ?></button></td>
                <td><a class="btn btn-primary" href="?close=<?php echo $arr[$i]["id"]."&page=$page&search=$search"; ?>">禁用该用户</a></td>
            </tr>
            <?php } ?>
        </table>
        <div class="pager">
            <?php
           pageList($pageCount, $page, "&search=$search");
            ?>
        </div>
    </div>
</div>
<?php
dialogHeader("show", "#", "记录查看","lg"); ?>
<div class="modal-body" style="min-height: 300px;overflow-y: auto">
<table id="table" class="table table-striped">

</table>
</div>
<?php
dialogFooter(0);
?>
<?php
dialogHeader("prizeDialog", "#", "兑换奖品",""); ?>
<div class="modal-body" style="min-height: 300px;overflow-y: auto">
    <table id="prizeTable" class="table table-striped">

    </table>
</div>
<?php
dialogFooter(0);
?>
<script>
    function expend(id) {
        $.post("getMessage.php",{expend:id},function (o) {
            tableEmpty()
            var js=eval(decodeURI(o));
            $("#table").append("<tr><th>金额</th><th>消费时间</th><th>奖品</th><th>领奖时间</th></tr>")
            for(var i=0;i<js.length;++i){
                $("#table").append("<tr><td>"+js[i].money+"</td><td>"+js[i].create_time+"</td><td>"+(js[i].allow_barter==false?"&times;":((js[i].finish_barter==true?js[i].name:"<button class='btn btn-primary' data-toggle='modal' data-target='#prizeDialog' type='button' onclick='getPrize("+js[i].id+")'>兑换奖品</button>")))+"</td><td>"+(js[i].barter_time==null?"--":js[i].barter_time)+"</td></tr>")

            }
        })
    }
    function bonus(id) {
        $.post("getMessage.php",{bonus:id},function (o) {
            tableEmpty()
            var js=eval(decodeURI(o));
            $("#table").append("<tr><td>金额</td><td>时间</td><td>备注</td></tr>")
            for(var i=0;i<js.length;++i){
                $("#table").append("<tr><td>"+js[i].money+"</td><td>"+js[i].create_time+"</td><td>"+(js[i].name==null?"数据错误":js[i].name)+"</td></tr>")

            }
        })
    }
    function tableEmpty() {
        $("#table").html("")
    }
    function getPrize(id) {
        $.post("getMessage.php",{getPrize:id},function (o) {
            prizeEmpty()
            var js=eval(o)
            $("#prizeTable").append("<tr><td>奖品名称</td><td>消费需求</td><td>奖品兑换</td></tr>")
            for(var i=0;i<js.length;++i){
                $("#prizeTable").append("<tr><td>"+js[i].name+"</td><td>"+js[i].expend+"</td><td><button type='button' class='btn btn-primary' onclick='prize("+id+","+js[i].id+","+js[i].user_id+")' "+(js[i].allow==true?">确认兑换":"disabled>消费不足")+"</button></td></tr>")
            }
        })
    }
    function prizeEmpty() {
        $("#prizeTable").html("")
    }
    function prize(expend_log_id, prize_id,user_id) {
        $.post("getMessage.php",{expendAddPrize1:expend_log_id,expendAddPrize2:prize_id},function (o) {
            $("#prizeTable").html(o)
            expend(user_id)
        })
    }
</script>
</body>
</html>
